package com.guanyua.eims.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.guanyua.eims.util.JsonUtil;
import com.guanyua.eims.util.RestUtil;
import com.guanyua.eims.pojo.*;
import com.guanyua.eims.pojo.goods.*;
import com.guanyua.eims.service.GoodsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 * 商品-信息 前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2020-10-18
 */
@Api(tags = "商品")
@ApiSupport(author = "任建波",order = 284)
@RestController
@RequestMapping("/goods-info")
@Validated
@Slf4j
public class GoodsInfoController {

    @Autowired
    GoodsInfoService goodsInfoService;

    @ApiOperation(value = "添加商品")
    @PostMapping(value = "add-goods")
    public Rest addGoods(@Valid GoodsInfoAddForm form) {
        GoodsInfo goodsInfo = new GoodsInfo();
        BeanUtils.copyProperties(form, goodsInfo);

        goodsInfoService.save(goodsInfo);
        return RestUtil.success();
    }

    @ApiOperation(value = "修改商品")
    @PostMapping(value = "update-goods")
    public Rest updateGoods(@Validated GoodsInfoUpdateForm form) {
        GoodsInfo goodsInfo = new GoodsInfo();
        BeanUtils.copyProperties(form, goodsInfo);

        goodsInfoService.updateById(goodsInfo);
        return RestUtil.success();
    }

    @ApiOperationSupport(author = "任建波")
    @ApiOperation(value = "获取商品分页列表")
    @GetMapping(value = "get-goods-page")
    public Rest getGoodsPage(@Valid GoodsInfoSearchForm form) {
        IPage<GoodsInfoDTO> goodsIPage = goodsInfoService.getGoodsPage(new Page(form.getCurrentPage(), form.getPageSize()));
        PageVO<GoodsInfoDTO> pageVO = JsonUtil.pageTransform(goodsIPage, GoodsInfoDTO.class);

        return RestUtil.success(pageVO);
    }

}

